/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.weapons.tool;

import com.minecraft.moonlake.MoonLakeAPI;
import com.minecraft.moonlake.api.packet.wrapper.PacketPlayOutChat;
import com.minecraft.moonlake.executor.Consumer;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponAbstractPlayerInteract;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponCloseable;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public class LongSword extends WeaponAbstractPlayerInteract implements WeaponCloseable {

    public LongSword() {
        super("longsword", "&b常青剑", Material.IRON_SWORD);
    }

    @Override
    public String[] getLore() {
        return new String[] {
                "&7右键使用消耗 &a30 &7耐久度恢复 &c2❤ &7血量",
                "&7冷却时间 &a35s"
        };
    }

    // 冷却时间 map
    private final static Map<String, Long> COOLDOWN_MAP = new HashMap<>();

    @Override
    protected void onInteract(PlayerInteractEvent event, Player player) {
        Long cache = COOLDOWN_MAP.get(player.getName());
        if(cache != null && System.currentTimeMillis() - cache < 35000) return; // 冷却还未结束则返回
        // 处理武器的耐久度减少
        ItemStack result = takeItemStackDurability(player.getItemInHand(), (short) 30);
        if(result == null) player.playSound(player.getLocation(), Sound.ITEM_BREAK, 10f, 1f); // 完全破坏则播放音效
        // 更新武器
        player.setItemInHand(result);
        playerUpdateInventory(player);
        healthPlayer(player, 4d); // 给予生命

        if(result == null) return; // 如果武器完全破坏则返回

        // 否则发送消息
        String cooldownEnter = getInstance().getMessage("WeaponCooldownEnter", displayName, 10);
        new PacketPlayOutChat(cooldownEnter, PacketPlayOutChat.Mode.HOTBAR).send(player);
        // 运行异步任务用来发送冷却完成的提示
        MoonLakeAPI.runTaskLaterAsync(getInstance(), () -> {
            if(!player.isOnline()) return; // 不在线则返回 (防止数据包抛出异常)
            String cooldownDone = getInstance().getMessage("WeaponCooldownDone", displayName);
            new PacketPlayOutChat(cooldownDone, PacketPlayOutChat.Mode.HOTBAR).send(player);
        }, 35 * 20L);
        // 将冷却时间 put 到缓存 map
        COOLDOWN_MAP.put(player.getName(), System.currentTimeMillis());
    }

    // 玩家退出事件
    public Consumer<PlayerQuitEvent> onQuit(Player player) {
        return event -> {
            if(COOLDOWN_MAP.containsKey(player.getName()))
                COOLDOWN_MAP.remove(player.getName());
        };
    }

    private ItemStack takeItemStackDurability(ItemStack itemStack, short count) {
        // 减少物品栈的耐久度
        short max = itemStack.getType().getMaxDurability(); // 获取最大耐久度
        if(itemStack.getDurability() + count >= max) return null; // 如果当前耐久度 + 耐久度大于等于则完全破坏返回 null
        else itemStack.setDurability((short) (itemStack.getDurability() + count)); // 否则减少耐久度
        return itemStack; // 返回还未完全破坏的物品栈
    }

    @Override
    public void close() {
        // 关闭能力清除冷却时间缓存
        COOLDOWN_MAP.clear();
    }
}
